//
//  CJExten+MJRefresh.swift
//  CJPack
//
//  Created by 陈警卫 on 2023/2/13.
//

import UIKit
import Foundation
import MJRefresh

open class CJRefreshNormalHeader: MJRefreshNormalHeader {
    
}

open class CJRefreshAutoNormalFooter : MJRefreshAutoNormalFooter {
    
}

// MARK: - header
public extension UIScrollView {
    
    /// 添加下拉方法
    @discardableResult
    func cj_mj_headerRefresh(_ closure: ((UIScrollView?) -> ())?) -> CJRefreshNormalHeader {
        let header = CJRefreshNormalHeader.init { [weak self] in
            if let closure = closure {
                closure(self)
            }
        }
        header.mj_h = header.mj_h + CJScreen.navBarHeight
        header.lastUpdatedTimeLabel?.isHidden = true
        mj_header = header
        return header
    }
    
    /// 设置header透明度
    func cj_mj_setHeader(alpha: CGFloat) {
        UIView.animate(withDuration: 0.25) { [weak self] in
            self?.mj_header?.alpha = alpha
        }
    }
    
    /// 设置header隐藏
    func cj_mj_setHeader(hidden: Bool) {
        mj_header?.isHidden = hidden
    }
    
    /// 移除header
    func cj_mj_removeHeader() {
        guard let mj_header = mj_header else { return }
        if mj_header.superview != nil {
            if mj_header.isRefreshing {
                mj_header.endRefreshing()
            }
            mj_header.removeFromSuperview()
        }
    }
    
    /// header开始刷新
    func cj_mj_headerBeginRefreshing() {
        mj_header?.beginRefreshing()
    }
    
    /// header停止刷新
    func cj_mj_headerEndRefreshing() {
        mj_header?.endRefreshing()
    }
    
    /// header是否隐藏
    var cj_mj_isHeaderHidden: Bool {
        if let isHidden = mj_header?.isHidden {
            return isHidden
        } else {
            return false
        }
    }
    
    /// header是否正在刷新
    var cj_mj_isHeaderRefreshing: Bool {
        if let isRefreshing = mj_header?.isRefreshing {
            return isRefreshing
        } else {
            return false
        }
    }
}


// MARK: - footer
public extension UIScrollView {
    
    /// 添加上拉方法
    @discardableResult
    func cj_mj_footerRefresh(_ noMoreText: String? , closure: ((UIScrollView?) -> ())?) -> CJRefreshAutoNormalFooter {
        let footer = CJRefreshAutoNormalFooter.init { [weak self] in
            if let closure = closure {
                closure(self)
            }
        }
        if let noMoreText = noMoreText {
            cj_mj_setFooter(footer: footer, title: noMoreText, state: .noMoreData)
        }
        mj_footer = footer
        return footer
    }

    /// 设置footer状态文本
    func cj_mj_setFooter(footer: CJRefreshAutoNormalFooter, title: String, state: MJRefreshState) {
        footer.setTitle(title, for: state)
    }
    

    /// 设置footer无更多数据
    func cj_mj_setFooter(noMore: Bool) {
        if noMore {
            mj_footer?.endRefreshingWithNoMoreData()
        } else {
            mj_footer?.resetNoMoreData()
        }
    }
    
    /// 设置header透明度
    func cj_mj_setFooter(alpha: CGFloat) {
        UIView.animate(withDuration: 0.25) { [weak self] in
            self?.mj_footer?.alpha = alpha
        }
    }
    
    /// 设置footer隐藏
    func cj_mj_setFooter(hidden: Bool) {
        mj_footer?.isHidden = hidden
    }

    /// 移除footer
    func cj_mj_removeFooter() {
        guard let mj_footer = mj_footer else { return }
        if mj_footer.superview != nil {
            if mj_footer.isRefreshing {
                mj_footer.endRefreshing()
            }
            mj_footer.removeFromSuperview()
        }
    }
    
    /// footer开始刷新
    func cj_mj_footerBeginRefreshing() {
        mj_footer?.beginRefreshing()
    }
    
    /// footer停止刷新
    func cj_mj_footerEndRefreshing() {
        mj_footer?.endRefreshing()
    }
    
    /// footer是否隐藏
    var cj_mj_isFooterHidden: Bool {
        if let isHidden = mj_footer?.isHidden {
            return isHidden
        } else {
            return false
        }
    }
    
    /// footer是否正在刷新
    var cj_mj_isFooterRefreshing: Bool {
        if let isRefreshing = mj_footer?.isRefreshing {
            return isRefreshing
        } else {
            return false
        }
    }
}
